package org.seven.jrdp.web.mgt.sys;

import java.io.File;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.seven.jrdp.biz.sys.model.SysUkey;
import org.seven.jrdp.biz.sys.service.SysUkeyService;
import org.seven.jrdp.commons.model.Result;
import org.seven.jrdp.commons.mybatis.model.PageRequest;
import org.seven.jrdp.commons.util.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/mgt/sys/ukey")
public class SysUkeyController {
	private static final String FILE_PATH = "static/plugins/ukey/setup.exe";
	@Autowired
	private SysUkeyService sysUkeyService;

	@GetMapping("/{view}")
	public ModelAndView view(@PathVariable String view) {
		return new ModelAndView("mgt/sys/ukey/ukey_" + view);
	}

	@PostMapping("/find")
	public PageInfo<Map<String, Object>> find(PageRequest pageRequest, HttpServletRequest request) {
		Map<String, Object> map = ServletUtils.getSearchParams(request);
		PageInfo<Map<String, Object>> page = sysUkeyService.find(map, pageRequest);
		return page;
	}

	@RequestMapping("/download")
	public void download(HttpServletRequest request, HttpServletResponse response) {
		File file = new File(request.getServletContext().getRealPath(FILE_PATH));
		ServletUtils.download(file, request, response);
	}

	@PostMapping("/bind")
	public Result bind(SysUkey sysUkey) {
		sysUkeyService.insert(sysUkey);
		return Result.SUCCESS;
	}

	@PostMapping("/unbind")
	public Result unbind(String id) {
		sysUkeyService.delete(id);
		return Result.SUCCESS;
	}
}